import 'package:flutter/material.dart';
import 'package:flutter_buisiness_2006a/bean/cart_info_bean_result.dart';
import 'package:flutter_buisiness_2006a/pages/cart/cart_count.dart';
import 'package:flutter_buisiness_2006a/provider/cart_provider.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

class CartItem extends StatefulWidget {
  const CartItem({Key? key, required this.item}) : super(key: key);
  final CartInfoBeanResult item;

  @override
  State<CartItem> createState() => _CartItemState();
}

class _CartItemState extends State<CartItem> {
  //购物车多选按钮
  Widget _cartCheckBox(CartInfoBeanResult item, context) {
    return Checkbox(
      value: item.isCheck, //当前的选中状态
      onChanged: (val) {
        item.isCheck = val; //拿到当前选中按钮的值,是否选中
        Provider.of<CartProvider>(context, listen: false)
            .changeCheckState(item);
        setState(() {});
      },
      activeColor: Colors.pink,
    );
  }

  //购物车商品图片
  Widget _cartImage(CartInfoBeanResult item) {
    return Container(
      width: ScreenUtil().setWidth(150).toDouble(),
      padding: const EdgeInsets.all(3),
      decoration:
          BoxDecoration(border: Border.all(width: 1, color: Colors.black12)),
      child: Image.network('${item.imagesUrl}'),
    );
  }

  //商品名称
  Widget _cartGoodsName(CartInfoBeanResult item) {
    return Container(
      width: ScreenUtil().setWidth(300).toDouble(),
      padding: const EdgeInsets.all(10),
      alignment: Alignment.topLeft,
      child: Column(
        children: [
          Text(
            item.goodsName!,
            maxLines: 2,
            style: TextStyle(fontSize: ScreenUtil().setSp(20).toDouble()),
          ),
          CartCount(item: item)
        ],
      ),
    );
  }

  //商品价格
  Widget _cartPrice(CartInfoBeanResult item, context) {
    return Container(
      width: ScreenUtil().setWidth(150).toDouble(),
      alignment: Alignment.centerRight,
      child: Column(
        children: [
          Text(
            '￥${item.price! * item.count!}元',
            style: TextStyle(fontSize: ScreenUtil().setSp(20).toDouble()),
          ),
          InkWell(
            onTap: () {
              Provider.of<CartProvider>(context, listen: false)
                  .deleteOneGood(item.goodsId!);
            },
            child: const Icon(
              Icons.delete_forever,
              color: Colors.black26,
              size: 25,
            ),
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.fromLTRB(5, 2, 5, 2),
      padding: const EdgeInsets.fromLTRB(5, 10, 5, 10),
      decoration: const BoxDecoration(
          color: Colors.white,
          border: Border(bottom: BorderSide(width: 1, color: Colors.black12))),
      child: Row(
        children: [
          _cartCheckBox(widget.item, context),
          _cartImage(widget.item),
          _cartGoodsName(widget.item),
          _cartPrice(widget.item, context),
        ],
      ),
    );
  }
}
